package com.ccibe.g16system.controller;

import com.ccibe.g16system.pojo.Customer;
import com.ccibe.g16system.pojo.Merchant;
import com.ccibe.g16system.pojo.form.LoginForm;
import com.ccibe.g16system.service.CustomerService;
import com.ccibe.g16system.service.MerchantService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserController {

    @Resource
    private CustomerService customerService;

    @Resource
    private MerchantService merchantService;

    @GetMapping("/login")
    public String loginPage(Model model) {
        LoginForm loginForm = new LoginForm();
        model.addAttribute("loginForm", loginForm);
        return "login";
    }

    @GetMapping("/register")
    public String register(Model model) {
        LoginForm loginForm = new LoginForm();
        model.addAttribute("loginForm", loginForm);
        return "register";
    }

    @PostMapping("/doLogin")
    public String doLogin(HttpServletRequest request, @ModelAttribute LoginForm loginForm, Model model) {
        String result;
        String redirectUrl;
        HttpSession session = request.getSession();
        if ("customer".equals(loginForm.getRole())) {
            Customer customer = customerService.login(loginForm);
            if(customer == null) {
                result = "";
            } else {
                result = "success";
                session.setAttribute("customer", customer);
            }
            redirectUrl = "/";
        } else if ("merchant".equals(loginForm.getRole())) {
            Merchant merchant = merchantService.login(loginForm);
            if(merchant == null) {
                result = "";
            } else {
                result = "success";
                session.setAttribute("merchant", merchant);
            }
            redirectUrl = "/manage?page=1&pageSize=5";
        } else {
            return "index";
        }
        if (result.isEmpty()) {
            model.addAttribute("error", true);
            model.addAttribute("loginForm", loginForm);
            return "login";
        }
        session.setAttribute("accountType", loginForm.getRole());
        model.addAttribute("successTitle", "登录成功");
        model.addAttribute("successMessage", "用户登录成功");
        model.addAttribute("redirectUrl", redirectUrl);
        return "redirect";
    }

    @PostMapping("/doRegister")
    public String doRegister(@ModelAttribute LoginForm loginForm, Model model) {
        if (!"customer".equals(loginForm.getRole())) {
            model.addAttribute("error", true);
            model.addAttribute("errorMessage", "表单错误");
            model.addAttribute("loginForm", loginForm);
            return "register";
        }
        boolean success = customerService.register(loginForm);
        if (success) {
            model.addAttribute("successTitle", "注册成功");
            model.addAttribute("successMessage", "用户注册成功");
            model.addAttribute("redirectUrl", "/");
        } else {
            model.addAttribute("error", true);
            model.addAttribute("errorMessage", "注册失败");
            model.addAttribute("loginForm", loginForm);
            return "register";
        }
        return "redirect";
    }

    @GetMapping("/doLogout")
    public String doLogout(HttpServletRequest request, Model model) {
        request.getSession().invalidate();
        model.addAttribute("successTitle", "注销成功");
        model.addAttribute("successMessage", "用户注销成功");
        model.addAttribute("redirectUrl", "/");
        return "redirect";
    }
}
